package steampunk.geom;

import java.awt.geom.Line2D;

import steampunk.art.Art;
import steampunk.art.LineArt;
import steampunk.utils.Mutil;

public class LineGeom extends BaseGeom {

	private int dx, dy;
	
	public LineGeom(int x1, int y1, int x2, int y2) {
		this.dx = x2 - x1;
		this.dy = y2 - y1;
		moveTo(x1, y1);
	}
	
	public double x1() {
		return getX();
	}
	public double x2() {
		return getX() + dx;
	}
	public double y1() {
		return getY();
	}
	public double y2() {
		return getY() + dy;
	}
	
	@Override
	public double bottom() {
		return Math.max(getY(), getY()+dy);
	}

	@Override
	public void bottom(double d) {
		if (dy>0)
			setY(d - dy);
		else
			setY(d);
	}
	@Override
	public Art getWireArt() {
		return new LineArt(this);
	}

	@Override
	public double left() {
		return Math.min(getX(), getX()+dx);
	}

	@Override
	public void left(double d) {
		if (dx > 0)
			setX(d);
		else
			setX(d - dx);
	}

	@Override
	public double right() {
		return Math.max(getX(), getX()+dx);
	}

	@Override
	public void right(double d) {
		if (dx > 0)
			setX(d - dx);
		else
			setX(d);
	}

	@Override
	public double top() {
		return Math.min(getY(), getY()+dy);
	}

	@Override
	public void top(double d) {
		if (dy > 0)
			setY(d);
		else
			setY(d-dy);
	}

	@Override
	public boolean containsPoint(double x, double y) {
		return Mutil.pointOnLine(x1(), y1(), x2(), y2(), x, y, 1);
	}

	@Override
	public boolean intersects(Intersectable other) {
		return other.intersectsLine(x1(), y1(), x2(), y2());
	}

	@Override
	public boolean intersectsCircle(double x, double y, double radius) {
		return Mutil.intLineCircle(x1(), y1(), x2(), y2(), x, y, radius);
	}

	@Override
	public boolean intersectsLine(double x1, double y1, double x2, double y2) {
		return Mutil.linesIntersect(x1(), y1(), x2(), y2(), x1, y1, x2, y2);
	}

	private final Line2D.Double interrectLine = new Line2D.Double(0, 0, 1, 1);
	@Override
	public boolean intersectsRect(int x, int y, int w, int h) {
		synchronized (interrectLine) {
			interrectLine.setLine(x1(), y1(), x2(), y2());
			return interrectLine.intersects(x, y, w, h);
		}
	}
	
}
